#!/bin/bash 
#
# This script will configure an Ubuntu server for use with RightScale
#
# Copied from:
# http://ec2ubuntu.googlecode.com/svn/!svn/bc/159/trunk/etc/init.d/rightscale-init
#

start() {
  ## First check to see if we were launched through RightScale. Exit and remove if not...
  user_data=`curl -s -S -f -L --retry 7 -w ' %{http_code}' http://169.254.169.254/latest/user-data`
  if `echo $user_data | grep -v "RS_token=" 1>/dev/null 2>&1` ; then
    ## Remove this init script so that it does not run again
    #remove
    exit 0 
  fi

  ## ok, we were launched through RightScale, so let's continue
  echo "Detected a RightScale instance..."
  echo "Beginning configuration..."

  ## figure out which version of RightScale to install...
  export rs_release=Ubuntu_`lsb_release -rs`

  ## Install some necessary packages
  echo "Installing necessary packages..."
  export DEBIAN_FRONTEND=NONINTERACTIVE
  apt-get update
  apt-get install -y binutils ruby1.8 sysv-rc-conf unzip ruby1.8-dev build-essential autoconf automake libtool logrotate rsync openssl ca-certificates libopenssl-ruby1.8

  ## Add rightscale customizations
  echo "Installing RightScale"
  curl -s -S -f -L --retry 7 -w ' %{http_code}' -o /tmp/rightscale_scripts.tgz http://s3.amazonaws.com/rightscale_scripts/rightscale_scripts_"$rs_release".tgz
  tar -xzf /tmp/rightscale_scripts.tgz -C /opt/
  ln -f /opt/rightscale/etc/init.d/rightscale /etc/init.d/rightscale
  chmod +x /opt/rightscale/etc/init.d/rightscale
  chmod +x /etc/init.d/rightscale
  mkdir -p /etc/rightscale.d
  update-rc.d rightscale start 98 2 3 4 5 . stop 1 0 1 6 .
  ln -sf /usr/bin/ruby1.8 /usr/bin/ruby
  ln -f /opt/rightscale/etc/motd /etc/motd
  echo $rs_release > /etc/rightscale-release

  ## Add rubygems 1.8
  echo "Installing RubyGems 1.8..."
  curl -s -S -f -L --retry 7 -w ' %{http_code}' -o /tmp/rubygems.tgz http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz
  mkdir -p /tmp/rubygems
  tar -xzvf /tmp/rubygems.tgz  -C $root/tmp/rubygems
  cd /tmp/rubygems/rubygems-1.3.1/
  ruby setup.rb --no-rdoc --no-ri
  ln -s /usr/bin/gem1.8  /usr/bin/gem
  gem source -a http://ec2-us-east-mirror.rightscale.com/rubygems/archive/latest/
  gem source -r http://mirror.rightscale.com
  gem install --no-rdoc --no-ri xml-simple net-ssh net-sftp s3sync 
  updatedb

  ## Add some links to keep things inline
  ln -s /usr/lib/site_ruby/aes /usr/local/lib/site_ruby/1.8/aes
  ln -s /usr/lib/site_ruby/ec2 /usr/local/lib/site_ruby/1.8/ec2
  ln -s /usr/bin/env /bin/env

  ## Insert 'ec2' as the cloud
  echo "ec2" > /etc/rightscale.d/cloud

  ## Enable root logins
  #echo "Enabling root logins..."
  #cp -f /home/ubuntu/.ssh/authorized_keys  /root/.ssh/.

  ## Remove the default sources.list becasue RightScale will create a better one anyways...
  rm -f /etc/apt/sources.list.d/amazon.list

  ## Remove this init script so that it does not run again
  remove

  ## Start the rightscale service  
  echo "Starting RightScale..."
  /etc/init.d/rightscale start
  echo "RightScale services started properly"

}

stop() {
  exit 0
}

remove() {
  my_name=`readlink -e $0`
  update-rc.d -f `basename $my_name` remove 
  rm -f $my_name
}


# See how we were called.
case "$1" in
  start)
    start >>/var/log/install 2>&1
    ;;
  stop)
    stop >>/var/log/install 2>&1
    ;;
  *)
    echo $"Usage: $0 {start|stop}"
    exit 1
esac

exit $?
